<?php

namespace application\admin\controller;

use ticky\request;
use ticky\schema;

class model_field extends base {

    private $modelid, $modeltable, $modelname;

    public function __construct() {
        parent::__construct();
        $this->modelid = isset($_GET['modelid']) ? intval($_GET['modelid']) : 1;
        $this->public_set_modelinfo();

        $this->assign('modelid', $this->modelid);
        $this->assign('modeltable', $this->modeltable);
        $this->assign('modelname', $this->modelname);
    }

    /**
     * 获取模型信息
     */
    private function public_set_modelinfo() {
        $data = db('model')->field('name,tablename')->where(array('modelid' => $this->modelid))->find();
        if ($data) {
            $this->modelname = $data['name'];
            $this->modeltable = $data['tablename'];
        } else {
            showmsg('模型不存在！', 'stop');
        }
    }

    /**
     * 首页
     */
    public function index() {
        $ret = db('model_field')->where(" modelid = '" . $this->modelid . "' ")->newpage();
        $this->assign('page', $ret['page']);
        $this->assign('items', $ret['items']);
        $this->display('sitemodel/model_field_list');
    }

    /**
     * 添加记录
     */
    public function add() {
        if (request::isPost()) {
            $data = $this->post_frm();
            if ($data['fieldtype'] == 'input' || $data['fieldtype'] == 'datetime') {
                schema::sql_add_field($this->modeltable, $data['field']);
            } else if ($data['fieldtype'] == 'textarea' || $data['fieldtype'] == 'images') {
                schema::sql_add_field_mediumtext($this->modeltable, $data['field']);
            } else if ($data['fieldtype'] == 'number') {
                schema::sql_add_field_int($this->modeltable, $data['field'], intval($data['defaultvalue']));
                $data['fieldtype'] = 'input';
            } else {
                schema::sql_add_field($this->modeltable, $data['field'], $data['defaultvalue'], $data['maxlength']);
            }
            db('model_field')->insert($data);
            showmsg('添加成功', '/admin/model_field/?modelid=' . $this->modelid);
        } else {
            $item = array();
            $item['css'] = 'form-control';
            $this->assign('postUrl', '/admin/model_field/add');
            $this->assign('action', L('add'));
            $this->assign('item', $item);
            $this->display('sitemodel/model_field_manage');
        }
    }

    /**
     * 更新记录
     */
    public function update() {
        if (request::isPost()) {
            $data = $this->post_frm();
            $id = request::post('fieldid', 0);
            db('model_field')->where(['fieldid' => $id])->update($data);
            showmsg('修改成功', '/admin/model_field/?modelid=' . $this->modelid);
        } else {
            $fieldid = request::get('fieldid', 0);
            $item = db('model_field')->where(['fieldid' => $fieldid])->find();
            $this->assign('postUrl', '/admin/model_field/update');
            $this->assign('action', L('edit'));
            $this->assign('item', $item);
            $this->display('sitemodel/model_field_manage');
        }
    }

    /**
     * 删除记录
     */
    public function delete() {
        $modelfield = db('modelfield');
        if ($_POST && is_array($_POST['ids'])) {
            foreach ($_POST['ids'] as $val) {
                $modelfield->delete(array('fieldid' => $val));
            }
        } else {
            $modelfield->delete(array('fieldid' => request::get('id', '0')));
        }
        showmsg(L('operation_success'), '/admin/model_field/index');
    }

    /**
     * 表单数据
     */
    private function post_frm() {
        $files = array('input', 'textarea', 'number', 'datetime', 'image', 'images', 'attachment', 'select', 'radio', 'checkbox');
        if (!in_array($_POST['fieldtype'], $files)) {
            showmsg('非法参数！', 'stop');
        }
        if (in_array($_POST['fieldtype'], array('select', 'radio', 'checkbox'))) {
            $_POST['setting'] = array2string(explode('|', rtrim($_POST['setting'], '|')));
        } elseif ($_POST['fieldtype'] == 'datetime') {
            $_POST['setting'] = $_POST['dateset'];
        } else {
            unset($_POST['setting']);
        }
        $arr = array();
        $arr['modelid'] = request::post('modelid', '');
        $arr['field'] = request::post('field', '');
        $arr['fieldtype'] = request::post('fieldtype', '');
        $arr['name'] = request::post('name', '');
        $arr['datatype'] = request::post('datatype', '');
        $arr['nullmsg'] = request::post('nullmsg', '');
        $arr['errormsg'] = request::post('errormsg', '');
        $arr['placeholder'] = request::post('placeholder', '');
        $arr['ajaxurl'] = request::post('ajaxurl', '');
        $arr['css'] = request::post('css', '');
        $arr['defaultvalue'] = request::post('defaultvalue', '');
        $arr['setting'] = request::post('setting', '');
        $arr['issystem'] = request::post('issystem', '0');
        $arr['listorder'] = request::post('listorder', '0');
        $arr['disabled'] = request::post('disabled', '0');
        $arr['type'] = request::post('type', '0');
        $arr['status'] = request::post('status', '1');
        return $arr;
    }

}
